import { getUserInfo, editUserInfo } from '@/api/my.js'
import { httpUpload } from '@/utils/http.js'
export default {
	data() {
		return {
			menuList: [
				{
					name: '头像',
					remark: '',
					handler: 'changeAvatar',
					isShow: true
				},
				{
					name: '手机号码',
					remark: '',
					isShow: true
				},
				{
					name: '昵称',
					remark: '填写',
					handler: 'goEditNickName',
					isShow: true
				},
				{
					name: '实名认证',
					remark: '去认证',
					handler: 'goEditName',
					isShow: true
				},
				{
					name: '个人介绍',
					remark: '填写',
					handler: 'goIntroduce',
					isShow: true
				},
				{
					name: '我的邮箱',
					remark: '填写',
					handler: 'goEditEmail',
					isShow: true
				},
				{
					name: '专业方向',
					remark: '',
					handler: 'chooseMajor',
					isShow: false
				}
			],
			modalShow: false,
			userInfo: {},
		}
	},
	methods: {
		//获取个人信息
		getUserInfo() {
			getUserInfo({}).then((res) => {
				let info = res.data
				this.userInfo = info
				if (info.is_major == 1) {
					this.menuList[6].isShow = true;
				} else {
					this.menuList[6].isShow = false;
				}

				this.filterMenu();
			})
		},
		//菜单数据处理
		filterMenu() {
			const { avatar, mobile, nickname, realname, introduce, email, major_direction, team_count, money, check_idcard, bio } = this.userInfo;

			const mobileTxt = `${mobile.slice(0, 3)}****${this.userInfo.mobile.slice(7, 11)}`;

			const params = [avatar, mobileTxt, nickname, realname, introduce, email, major_direction, team_count, money];

			this.menuList.forEach((ele, index) => {
				if (index === 3) {
					ele.remark = check_idcard === 1 ? '已认证' : check_idcard === 2 ? '申请中' : '去认证';
				} else if (index === 4) {
					ele.remark = bio;
				} else {
					ele.remark = params[index];
				}
			});
		},


		//点击事件
		menuClick(item) {
			if (item.handler) {
				this[item.handler]();
			} else if (item.url) {
				this.$used.href(item.url);
			}
		},

		//显示头像弹窗
		changeAvatar() {
			let that = this;
			uni.showActionSheet({
				itemList: ['查看头像', '更换头像'],
				success: function (res) {
					if (res.tapIndex == 0) {
						that.$used.previewImg(0, [that.userInfo.avatar]);
					} else {
						that.chooseImage();
					}
				},
				fail: function (res) {
					console.log(res.errMsg);
				}
			});
		},

		//从文件夹选择头像图片
		chooseImage() {
			uni.chooseImage({
				count: 1, //默认9
				sizeType: ['original', 'compressed'],
				sourceType: ['album', 'camera'],
				success: (res) => {
					if (res.tempFilePaths.length > 0) {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							this.toUpload(res.tempFilePaths[i]);
						}
					}
				}
			});
		},

		//上传图片
		toUpload(img) {
			httpUpload(this.$paths.commonUpload, img).then((res) => {
				if (res.code == 1) {
					//更新
					this.upUserInfo(res.data.url);
				} else {
					this.$tools.showToast(res.msg);
				}
			})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},

		//更新个人信息
		upUserInfo(url) {
			let params = {
				avatar: url,
				rLoading: true
			};
			editUserInfo(params).then(res => {
				if (res.code == 1) {
					this.$used.msg('修改成功');
					this.getUserInfo();
				} else {
					this.$used.msg(msg);
				}
			})
		},

		//修改昵称
		goEditNickName() {
			this.$used.href(`./nickNameEdit?type=0&name=${this.userInfo.nickname}`);
		},

		//实名认证
		goEditName() {
			if (this.userInfo.check_idcard == 1) {
				this.$used.msg('已认证');
			} else if (this.userInfo.check_idcard == 2) {
				this.$used.msg('申请中');
			} else {
				this.$used.href('./realName');
			}
		},

		//个人介绍
		goIntroduce() {
			this.$used.href(`./nickNameEdit?type=1&name=${this.userInfo.bio}`);
		},

		//修改邮箱
		goEditEmail() {
			this.$used.href(`./emailEdit?email=${this.menuList[5].remark}`);
		},




	},
	computed: {

	},
	onShow() {
		this.getUserInfo()
	},
}